package com.base.app;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.ala.libmodbusapi.ModbusOperatio;
import com.base.app.base.BaseActivity;
import com.base.app.compon.TipsDialog;
import com.base.app.enve.ExitApp;
import com.base.app.fg.FgHome;
import com.base.app.util.ReStartException;
import com.base.app.util.SerialPortFinder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.greenrobot.eventbus.android.BuildConfig;

import java.util.ArrayList;

//import me.yokeyword.fragmentation.BuildConfig;
//import me.yokeyword.fragmentation.Fragmentation;
//import me.yokeyword.fragmentation.SupportActivity;

public class MainActivity extends BaseActivity implements AdapterView.OnItemSelectedListener, View.OnClickListener {


    String TAG="MainActivity";

    Button bu_opendev, bu_exit;
    Button bu_charaddr;

    Button bu_openled, bu_closeled;
    Button bu_uptime, quer_dev;

    Button bu_lig_max, bu_lig_min;

    Button bu_app_reset;
    TextView te_dev_para;

    Spinner sp_serial;
    Spinner sp_addr;
    Spinner sp_new_addr;
    Spinner sp_led;

    String serialDev="ttyS1";
    int defAddr=1;
    int newAddr=1;
    int defSw=1;

    int brig=10;

    ArrayList<String> sp_addr_list= new ArrayList<>();
    ArrayList<String> sp_addr_new_list= new ArrayList<>();
    ArrayList<String> sp_led_list= new ArrayList<>();
    ArrayList<String> sp_serial_list= new ArrayList<>();

    ModbusOperatio modbusOperatio;

    @Override
    protected int setViewId() {
        return R.layout.layout_fg_main;
    }

    @Override
    protected void beforeInitView() {

    }

    @Override
    protected void initView() {
        /*
         *   evenbus注册问题：
         *   1   只有需要监听接收消息的 Activity/Fragment 才需要注册
         *   2   发送事件的类也不需要注册，除非他需要监听接收消息
         * */

        EventBus.getDefault().register(this);

        ArrayAdapter<String> adapteraddr=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, sp_addr_list);
        sp_addr = findViewById(R.id.sp_addr);
        sp_addr.setAdapter(adapteraddr);
        // sp_addr.setSelection(0);
        sp_addr.setOnItemSelectedListener(this);

        ArrayAdapter<String> aadp_new_addr=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, sp_addr_new_list);
        sp_new_addr = findViewById(R.id.sp_new_addr);
        sp_new_addr.setAdapter(aadp_new_addr);
        // sp_addr.setSelection(0);
        sp_new_addr.setOnItemSelectedListener(this);

        ArrayAdapter<String> aadp_led=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, sp_led_list);
        sp_led = findViewById(R.id.sp_led);
        sp_led.setAdapter(aadp_led);
        // sp_addr.setSelection(0);
        sp_led.setOnItemSelectedListener(this);


        ArrayAdapter<String> aadp_serial=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, sp_serial_list);
        sp_serial = findViewById(R.id.sp_serial);
        sp_serial.setAdapter(aadp_serial);
        sp_serial.setOnItemSelectedListener(this);



        bu_opendev=findViewById(R.id.bu_opendev);
        bu_opendev.setOnClickListener(this);
        bu_exit=findViewById(R.id.bu_exit);
        bu_exit.setOnClickListener(this);
        bu_charaddr=findViewById(R.id.bu_charaddr);
        bu_charaddr.setOnClickListener(this);
        bu_openled=findViewById(R.id.bu_openled);
        bu_openled.setOnClickListener(this);
        bu_closeled=findViewById(R.id.bu_closeled);
        bu_closeled.setOnClickListener(this);
        bu_uptime=findViewById(R.id.bu_uptime);
        bu_uptime.setOnClickListener(this);
        quer_dev=findViewById(R.id.quer_dev);
        quer_dev.setOnClickListener(this);
        bu_lig_max=findViewById(R.id.bu_lig_max);
        bu_lig_max.setOnClickListener(this);
        bu_lig_min=findViewById(R.id.bu_lig_min);
        bu_lig_min.setOnClickListener(this);
        bu_app_reset=findViewById(R.id.bu_app_reset);
        bu_app_reset.setOnClickListener(this);



        te_dev_para=findViewById(R.id.te_dev_para);
    }

    @Override
    protected void initData() {
        initMyData();
    }

    @Override
    protected void initListener() {

    }

    private void initMyData(){
        sp_addr_list=new ArrayList<String>();
        for(int i=0; i<200; i++){
            sp_addr_list.add("当前地址 "+(i+1));
        }

        sp_addr_new_list=new ArrayList<String>();
        for(int i=0; i<200; i++){
            sp_addr_new_list.add("新的地址 "+(i+1));
        }

        sp_led_list=new ArrayList<String>();
        for(int i=0; i<4; i++){
            sp_led_list.add("开关个数 "+(i+1));
        }
        sp_serial_list = new SerialPortFinder().getAllDevicesPath_ArrayList();
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
        Log.i(TAG, "onDestroy: exit app");
        finish();
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void evenExitApp(ExitApp exitApp){

        Log.i(TAG,exitApp.getSignal() + " " + exitApp.getStr());
        if(exitApp.getSignal()==0) {
            Log.i(TAG, " Exit BaseApplication ");
            finish();
        }else if(exitApp.getSignal()==1){
            Log.i(TAG, " Reset BaseApplication ");
            ReStartException.restartNow(this);
        }
    }

    TipsDialog tipsDialog;
    public void showLoading(String msg) {
        if(tipsDialog==null){
            tipsDialog = new TipsDialog(this);
            tipsDialog.setOnClickBottomListener(new TipsDialog.OnClickBottomListener() {
                @Override
                public void onClickCancel() {
                    closeLoading();
                }
            });
        }
        tipsDialog.show();
        tipsDialog.setText_tips(msg);
    }

    public void closeLoading() {
        if(tipsDialog!=null){
            tipsDialog.dismiss();
            tipsDialog=null;
        }
    }

    @Override
    public void onClick(View v) {
        int gid = v.getId();
        if(gid == R.id.bu_opendev){
            if(modbusOperatio==null){
                modbusOperatio=ModbusOperatio.newInstance(serialDev, 9600);
                modbusOperatio.initDev();
            }
        }else if(gid == R.id.bu_exit){
            EventBus.getDefault().post(new ExitApp(0, "exitApp"));
        }else if(gid == R.id.bu_app_reset){
            EventBus.getDefault().post(new ExitApp(1, "resetApp"));
        }else if(gid == R.id.bu_charaddr){
            if(modbusOperatio!=null){
                if(-1 == modbusOperatio.md_set_addr(defAddr, newAddr)){
                    te_dev_para.setText("失败");
                    Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show();
                }else {
                    te_dev_para.setText("成功");
                    Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
                }
            }
        }else if(gid == R.id.bu_openled){
            if(modbusOperatio!=null){
                if(-1 == modbusOperatio.md_led_1_open(defAddr)){
                    te_dev_para.setText("失败");
                    Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show();
                }else {
                    te_dev_para.setText("成功");
                    Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
                }
            }
        }else if(gid == R.id.bu_closeled){
            if(modbusOperatio!=null){
                if(-1 == modbusOperatio.md_led_1_close(defAddr)){
                    te_dev_para.setText("失败");
                    Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show();
                }else {
                    te_dev_para.setText("成功");
                    Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
                }
            }
        }else if(gid == R.id.bu_uptime){
            if(modbusOperatio!=null){
                if(-1 == modbusOperatio.md_up_time(defAddr)){
                    te_dev_para.setText("失败");
                    Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show();
                }else {
                    te_dev_para.setText("成功");
                    Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
                }
            }
        }else if(gid == R.id.quer_dev){
            if(modbusOperatio!=null){
                if(-1 == modbusOperatio.md_query_dev(defAddr)){
                    te_dev_para.setText("失败");
                    Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show();
                }else {
                    te_dev_para.setText("成功");
                    Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
                }
                te_dev_para.setText(modbusOperatio.getStrDevPara());
            }
        }else if(gid == R.id.bu_lig_max){
            if(modbusOperatio!=null){
                if(-1 == modbusOperatio.md_lig_para(defAddr, brig)){
                    te_dev_para.setText("失败");
                    Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show();
                }else {
                    te_dev_para.setText("成功");
                    Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
                    brig+=10;
                    if(brig>=100){
                        brig=100;
                    }
                }
                te_dev_para.setText(modbusOperatio.getStrDevPara());
            }
        }else if(gid == R.id.bu_lig_min){
            if(modbusOperatio!=null){
                if(-1 == modbusOperatio.md_lig_para(defAddr, brig)){
                    te_dev_para.setText("失败");
                    Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show();
                }else {
                    te_dev_para.setText("成功");
                    Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
                    brig-=10;
                    if(brig<=0){
                        brig=0;
                    }
                }
                te_dev_para.setText(modbusOperatio.getStrDevPara());
            }
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        int vid = parent.getId();
        if(vid == R.id.sp_addr){
            defAddr=position+1;
        }else if(vid == R.id.sp_new_addr){
            newAddr=position+1;
        }else if(vid == R.id.sp_led){
            defSw=position+1;
        }else if(vid == R.id.sp_serial){
            serialDev = sp_serial_list.get(position);
        }

        Log.i(TAG, "onItemSelected: defAddr=" + defAddr
                + " newAddr=" + newAddr
                + " defSw=" + defSw
                + " serialDev="+serialDev);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
